﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * Original source PushButton Engine:
 * Copyright (C) 2009 PushButton Labs, LLC
 * For more information see http://www.pushbuttonengine.com
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using System.Collections;
using AAEngine.Engine;
using AAEngine.Engine.Components;
using AAEngine.Engine.Entities;

namespace AAEngine.Animation
{
    public class AnimatorComponent : AnimatedComponent
    {
        // private Dictionary<string, Animator> _animations;
        private Hashtable _animations;
        private bool _autoPlay = true;
        private string _defaultAnimation = "Idle";
        private PropertyReference _propertyReference = null;
        private Animator _currentAnimation = null;

        /// <summary>
        /// A list of all the animation that can be played by this component.
        /// </summary>
        public Hashtable Animations
        {
            get { return _animations; }
            set { _animations = value; }
        }

        /// <summary>
        /// Whether or not to start the animation when the component is registered.
        /// </summary>
        public bool AutoPlay
        {
            get { return _autoPlay; }
            set { _autoPlay = value; }
        }

        /// <summary>
        /// The name of the animation to automatically start playing when the component
        /// is registered.
        /// </summary>
        public string DefaultAnimation
        {
            get { return _defaultAnimation; }
            set { _defaultAnimation = value; }
        }

        /// <summary>
        /// A reference to the property that will be animated.
        /// </summary>
        public PropertyReference Reference
        {
            get { return _propertyReference; }
            set { _propertyReference = value; }
        }

        public override void OnFrame(float deltaTime)
        {
            if (_currentAnimation != null)
            {
                _currentAnimation.Animate(deltaTime);

                Owner.SetProperty(_propertyReference, (object)_currentAnimation.CurrentValue);
            }

            base.OnFrame(deltaTime);
        }

        /// <summary>
        /// Plays an animation that is on this component.
        /// </summary>
        /// <param name="animation">The name of the animation in the Animations dictionary
        /// to play.</param>
        /// <param name="startValue">The value to start at. If this is null (the default), the
        /// start value won't be changed.</param>
#if WINDOWS_PHONE || XBOX
        public void Play(string animation)
        {
            Play(animation, null);
        }

        public void Play(string animation, object startValue)
#else
        public void Play(string animation, object startValue = null)
#endif
        {
            if (_currentAnimation != null && _currentAnimation.IsAnimating)
            {
                _currentAnimation.Stop();
            }

            if (!_animations.ContainsKey(animation))
            {
                _currentAnimation = null;
                return;
            }

            _currentAnimation = _animations[animation] as Animator;
            if (startValue != null)
            {
                _currentAnimation.StartValue = startValue;
            }

            _currentAnimation.Reset();
            _currentAnimation.Play();
        }

        public override void OnReset()
        {
            if (!_autoPlay || _currentAnimation != null)
            {
                return;
            }

            Play(_defaultAnimation);

            base.OnReset();
        }
    }
}
